package applicationFunction.socks.nwct;

import java.io.FileDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.net.SocketImpl;
import java.util.logging.Logger;

public class Util {
    private static Logger logger = Logger.getLogger("applicationFunction.socks.nwct.Util");
    /**
     * 获取socket的文件描述符
     * @param socket
     * @throws Exception
     */
    public static FileDescriptor getFD(Socket socket)throws Exception{
        Field implField = Socket.class.getDeclaredField("impl");
        implField.setAccessible(true);
        SocketImpl socketImpl = (SocketImpl) implField.get(socket);

        Method method = SocketImpl.class.getDeclaredMethod("getFileDescriptor");
        method.setAccessible(true);
        FileDescriptor fd = (FileDescriptor)method.invoke(socketImpl);
        Field fdField = FileDescriptor.class.getDeclaredField("fd");
        fdField.setAccessible(true);
        return fd;
    }

}
